package cn.itcast.bos.web.action.base;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;

@Namespace("/")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea> {
	private static final long serialVersionUID = 1L;
	@Autowired
	private SubAreaService subAreaService;
	@Autowired
	private FixedAreaService fixedAreaService;
	@Autowired
	private AreaService areaService;
	
	//手动添加分区
	@Action(value = "subArea_add", results = {
			@Result(name = "success", location = "./pages/base/sub_area.html", type = "redirect") })
	public String add(){
		subAreaService.add(model);
		return SUCCESS;
	}
	//删除
	private String idsStr;
	public void setIdsStr(String idsStr) {
		this.idsStr = idsStr;
	}
	@Action(value = "subArea_del", results = {
			@Result(name = "success", location = "./pages/base/sub_area.html", type = "redirect") })
	public String del(){
		subAreaService.del(idsStr);
		return SUCCESS;
	}
	
	// 批量导入分区
	private String file;

	public void setFile(String file) {
		this.file = file;
	}

	@Action(value = "subArea_batchImport", results = {
			@Result(name = "success", location = "./pages/base/sub_area.html", type = "redirect") })
	public String batchImport() throws IOException {
		// 声明一个集合来存放SubArea
		List<SubArea> subAreas = new ArrayList<SubArea>();
		// 加载.xlsx文件
		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
		// 获得第一个sheet
		XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
		// 遍历sheet
		for (Row row : xssfSheet) {
			// 跳过第一行数据,表头
			if (row.getRowNum() == 0) {
				continue;
			}
			// 跳过表中空行
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			// 封装数据
			SubArea subArea = new SubArea();
			subArea.setId(row.getCell(0).getStringCellValue());

			FixedArea fixedArea = fixedAreaService.findOne(row.getCell(1).getStringCellValue());
			subArea.setFixedArea(fixedArea);
			Area area = areaService.findOne(row.getCell(2).getStringCellValue());
			subArea.setArea(area);

			subArea.setKeyWords(row.getCell(3).getStringCellValue());
			subArea.setStartNum(row.getCell(4).getStringCellValue());
			subArea.setEndNum(row.getCell(5).getStringCellValue());
			subArea.setSingle(row.getCell(6).getStringCellValue().charAt(0));
			subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());
			subAreas.add(subArea);
		}
		subAreaService.addBatch(subAreas);
		xssfWorkbook.close();
		return SUCCESS;
	}

	// 带条件分页查询
	@Action(value = "subArea_pageQuery")
	public void pageQuery() throws IOException {
		// 声明分页对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 声明条件对象
		Specification<SubArea> specification = new Specification<SubArea>() {
			List<Predicate> list = new ArrayList<Predicate>();

			@Override
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 多表条件
				// 省
				Join<SubArea, Area> areaJoin = root.join("area", JoinType.INNER);
				if (model.getArea() != null && StringUtils.isNotBlank(model.getArea().getProvince())) {
					Predicate p1 = cb.equal(areaJoin.get("province").as(String.class), model.getArea().getProvince());
					list.add(p1);
				}
				// 市
				if (model.getArea() != null && StringUtils.isNotBlank(model.getArea().getCity())) {
					Predicate p2 = cb.equal(areaJoin.get("city").as(String.class), model.getArea().getCity());
					list.add(p2);
				}
				// 区
				if (model.getArea() != null && StringUtils.isNotBlank(model.getArea().getDistrict())) {
					Predicate p3 = cb.equal(areaJoin.get("district").as(String.class), model.getArea().getDistrict());
					list.add(p3);
				}
				// 定区ID
				Join<SubArea, FixedArea> fixedAreajoin = root.join("fixedArea", JoinType.INNER);
				if (model.getFixedArea() != null && StringUtils.isNotBlank(model.getFixedArea().getId())) {
					Predicate p4 = cb.equal(fixedAreajoin.get("id").as(String.class), model.getFixedArea().getId());
					list.add(p4);
				}
				// 单表条件
				if (StringUtils.isNotBlank(model.getKeyWords())) {
					Predicate p5 = cb.like(root.get("keyWords").as(String.class), "%" + model.getKeyWords() + "%");
					list.add(p5);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<SubArea> pageData = subAreaService.pageQuery(specification, pageable);
		pageDataJsonToWeb(pageData);
	}
	
}
